home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / programming / source / modem_1.2.lha / modem / modem.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-12  |  4.3 KB  |  208 lines

  1. /*
  2.  * Modem setup/dialer program.  Usage is: modem [-d Device] [-n number]
  3.  * 
  4.  * $Revision: 1.4 $ $Author: srn $ $Log: modem.c,v $
  5.  * Revision 1.4  1993/12/12  14:06:07  srn
  6.  * Added BAUD optin
  7.  *
  8.  * Revision 1.3  1993/11/20  14:39:30  srn
  9.  * Fixed version string.
  10.  *
  11.  * Revision 1.2  1993/10/09  11:58:15  srn
  12.  * Fixed bug with extraneous characters after ^C.
  13.  *
  14.  * Revision 1.1  1993/09/04  11:42:33  srn
  15.  * Initial revision
  16.  *
  17.  */
  18.  
  19. /*
  20.         This program is copyright 1990, 1993 Stephen Norris. 
  21.         May be freely distributed provided this notice remains intact.
  22. */
  23.  
  24.  
  25. #include <exec/types.h>
  26. #include <devices/serial.h>
  27. #include <dos/dos.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31.  
  32. #include <clib/dos_protos.h>
  33.  
  34. #include "t:DateHeader.h"
  35. #define VERSIONTEXT "Modem 1.2 " __AMIGADATE__;
  36. const char Version[] = "$VER:" VERSIONTEXT;
  37.  
  38. const char Template[] = "DEVICE,UNIT/N,BAUD/N";
  39. char   *Device = "serial.device";
  40. int     Unit = 0;
  41. int    Baud = 0;    /* Baud rate requested from command line. */
  42.  
  43. int     SerOpen = FALSE;    /* Flag set if the serial device is open. */
  44.  
  45. struct MsgPort *SerialMP = NULL;    /* Port for replies to io messages to go
  46.  
  47.                        * to. */
  48. struct IOExtSer *SerialIO = NULL;    /* Data structure used for
  49.  
  50.                      * messages to device. */
  51. struct IOExtSer *IncomingIO = NULL;    /* Structure for incoming
  52.  
  53.                      * characters. */
  54. void    OpenAll( void );
  55. void    CloseAll( void );
  56.  
  57. void
  58. OpenAll()
  59. {
  60.     if ((SerialMP = (struct MsgPort *) CreatePort(0, 0)) == NULL) {
  61.         printf("Can't create message port.\n");
  62.         CloseAll();
  63.         exit(30);
  64.     }
  65.     if ((SerialIO = (struct IOExtSer *) CreateExtIO(SerialMP, sizeof(struct IOExtSer))) == NULL) {
  66.         printf("Can't allocate storage for ExtIO.\n");
  67.         CloseAll();
  68.         exit(30);
  69.     }
  70.     if ((IncomingIO = (struct IOExtSer *) CreateExtIO(SerialMP, sizeof(struct IOExtSer))) == NULL) {
  71.         printf("Can't allocate storage for ExtIO.\n");
  72.         CloseAll();
  73.         exit(30);
  74.     }
  75.     SerialIO->io_SerFlags = SERF_SHARED;    /* Turn on SHARED Mode */
  76.  
  77.     if ((SerOpen = !OpenDevice(Device, (long) Unit, SerialIO, 0L)) == NULL) {
  78.         printf("Can't open %s unit %ld.\n", Device, (long) Unit);
  79.         CloseAll();
  80.         exit(30);
  81.     }
  82.  
  83.     if (Baud != 0){
  84.         /* Set baud rate if needed. */
  85.         SerialIO->io_Baud = Baud;
  86.         SerialIO->IOSer.io_Command = SDCMD_SETPARAMS;
  87.         if (DoIO(SerialIO) != NULL) {
  88.             printf("Unable to set baud rate.\n");
  89.         }
  90.     }
  91.     memcpy(IncomingIO, SerialIO, sizeof(struct IOExtSer));
  92.  
  93.     if (!conInit()){
  94.         printf("Console setup failed.\n");
  95.         CloseAll();
  96.         exit(30);
  97.     }
  98. }
  99.  
  100. void
  101. CloseAll()
  102. {
  103.  
  104.     conClose();
  105.  
  106.     if (SerOpen)
  107.         CloseDevice(SerialIO);
  108.  
  109.     if (IncomingIO)
  110.         DeleteExtIO(IncomingIO);
  111.  
  112.     if (SerialIO)
  113.         DeleteExtIO(SerialIO);
  114.  
  115.     if (SerialMP)
  116.         DeletePort(SerialMP);
  117. }
  118.  
  119. void
  120. Break()
  121. {
  122.     printf("*** Break\n");
  123.     while (WaitForChar(Input(), 10))
  124.         getchar();
  125.     CloseAll();
  126.     exit(30);
  127. }
  128.  
  129. void
  130. main(int argc, char *argv[])
  131. {
  132.     struct    RDArgs *Args;
  133.     long    *ArgRes[3] = {0, 0, 0};
  134.     int     Done = 0;
  135.  
  136.     /* Handle arguments. */
  137.     if ((Args = ReadArgs(Template, ArgRes, NULL)) == NULL){
  138.         printf("Usage: %s\n", Template);
  139.         CloseAll();
  140.         exit(30);
  141.     }
  142.  
  143.     if (ArgRes[0] != 0){
  144.         Device = strdup((char *)ArgRes[0]);
  145.     }
  146.     if (ArgRes[1] != 0){
  147.         Unit = *ArgRes[1];
  148.     }
  149.     if (ArgRes[2] != 0){
  150.         Baud = *ArgRes[2];
  151.     }
  152.  
  153.     FreeArgs(Args);
  154.  
  155.     OpenAll();
  156.     onbreak(Break);
  157.  
  158.     while (!Done) {
  159.         char    Buffer;
  160.         int     length;
  161.         char   *InBuffer;
  162.  
  163.         if (WaitForChar(Input(), 100000)) {
  164.             Buffer = getchar();
  165.             if (Buffer == '\n')
  166.                 Buffer = '\r';
  167.  
  168.             SerialIO->IOSer.io_Command = CMD_WRITE;
  169.             SerialIO->IOSer.io_Length = 1;
  170.             SerialIO->IOSer.io_Data = (APTR) &Buffer;
  171.             if (DoIO(SerialIO) != NULL) {
  172.                 printf("Write failed with error %d.\n", SerialIO->IOSer.io_Error);
  173.                 CloseAll();
  174.                 exit(30);
  175.             }
  176.         }
  177.         /* Serial input? */
  178.         IncomingIO->IOSer.io_Command = SDCMD_QUERY;
  179.         IncomingIO->IOSer.io_Actual = 0;
  180.         if (DoIO(IncomingIO) != NULL) {
  181.             printf("Failed SDCMD_QUERY\n");
  182.             CloseAll();
  183.             exit(30);
  184.         }
  185.         if ((length = IncomingIO->IOSer.io_Actual) > 0) {
  186.             if ((InBuffer = malloc(length+1)) == NULL) {
  187.                 printf("Out of memory\n");
  188.                 CloseAll();
  189.                 exit(30);
  190.             }
  191.             IncomingIO->IOSer.io_Command = CMD_READ;
  192.             IncomingIO->IOSer.io_Length = length;
  193.             IncomingIO->IOSer.io_Data = InBuffer;
  194.             if (DoIO(IncomingIO) != NULL) {
  195.                 printf("Failed read.\n");
  196.                 free(InBuffer);
  197.                 CloseAll();
  198.                 exit(30);
  199.             }
  200.             Write (Output(), InBuffer, length);
  201.  
  202.             free(InBuffer);
  203.         }
  204.     }
  205.     CloseAll();
  206.     exit(0);
  207. }
  208.